package com.web.music.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.web.music.interfaces.ArtistService;
import com.web.music.interfaces.PersistenceHelper;
import com.web.music.model.PersistentEntity;
import com.web.music.model.Artist;
import com.web.music.model.Singer;
import com.web.music.model.MusicBand;

@Transactional
public class ArtistServiceImpl implements ArtistService {
	
	private PersistenceHelper persistence;
	
	public void save(Artist artist) {
		persistence.save(artist);
	}

	public List<Artist> findAll() {
		List<PersistentEntity> all = persistence.findAll(Artist.class);
		List<Artist> artists = new ArrayList<Artist>(); 
		for (PersistentEntity e: all){
			artists.add((Artist)e);
		}
		return artists; 
	}
	
	public List<Artist> findAll(String searchMode) {
		List<PersistentEntity> all;
		if (searchMode.equals("singer")){
			all = persistence.findAll(Singer.class);
		}
		else
		{
			all = persistence.findAll(MusicBand.class);
		}
		
		List<Artist> artists = new ArrayList<Artist>(); 
		for (PersistentEntity e: all){
			artists.add((Artist)e);
		}
		return artists; 
	}	
	
	public Artist getById(Long id) {
		return (Artist)persistence.getById(Artist.class, id);
	}

	public PersistenceHelper getPersistence() {
		return persistence;
	}

	public void setPersistence(PersistenceHelper persistence) {
		this.persistence = persistence;
	}
}